<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>仓库区域设置</title>
</head>
<body>
<div style="width:100%">

    <div class="items clearfix">
        <h2 style="" class="tal">正在操作：<!--{if $storeID}-->修改仓库区域<!--{else}-->添加仓库区域<!--{/if}--></h2>
        <div class="items2">

            <form id="storeRegion_form" name="storeRegion_form" onsubmit="return false;">
                <table>
                    <tbody>
                    <tr>
                        <td colspan="6">
                            <input type="hidden" name="storeID" value="<!--{$storeID}-->">
                            <input type="hidden" name="editFlg" value="<!--{$editFlg}-->">
                        </td>
                    </tr>

                    </tbody>
                </table>

                <table >
                    <tbody>
                    <tr>
                        <td class="tar" style="padding-bottom: 30px;">仓库名称：</td>
                        <td colspan="5" style="padding-bottom: 30px;" >
                            <!--{$storeName}-->
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">所选区域：(省)</td>
                        <td colspan="5" id="province" >
                            <div id="provinceDiv" >
                                <!--{$provinceList}-->
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">(市)</td>
                        <td  colspan="5"  id="city">
                            <div id="cityDiv" >
                                <!--{$cityList}-->
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">(区)</td>
                        <td  colspan="5"  id="region">
                            <div id="regionDiv" >
                                <!--{$regionList}-->
                            </div>
                        </td>
                    </tr>
                    <tr style="height:20px">
                        <td class="tar" style="padding-top:20px;width:180px;">省：</td>
                        <td style="width:140px;">
                            <select id="provinceList" name="provinceList" size="20" size="20" style="height:150px;width:120px;">
                                <option value="">请选择</option>
                                    <!--{foreach from=$province item=line}-->
                                <option value="<!--{$line->id}-->"><!--{$line->name}--></option>
                                    <!--{/foreach}-->
                            </select>
                        </td>
                        <td class="tar" style="width:110px;">市：</td>
                        <td style="width:140px;">
                            <select id="cityList" name="cityList" size="20" size="20" style="height:150px;width:120px;">
                                <option value="">请选择</option>
                            </select>
                        </td>
                        <td class="tar" style="width:110px;">区：</td>
                        <td style="width:160px;">
                            <select id="regionList" name="regionList" size="20" size="20" style="height:150px;width:120px;">
                                <option value="">请选择</option>
                            </select>
                        </td>
                        <td >
                            <input type="button" value="添加" class="btn" onclick="addSelectRegion();">
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" align="right" class="td_title">&nbsp;</td>
                        <td colspan="5" style="padding-top:10px;">
                            <input type="button"  id="allAddBtn" name="allAddBtn" onclick="doAddAll();" class="btn" value="全部添加">
                        </td>
                    </tr>
                    </tbody>
                </table>

            </form>

        </div>
    </div>

    <div class="bottom_fixbox">
        <div class="rectbox tac">
            <input type="button" class="btn" onclick="regionModify.check();" value="<!--{if $editFlg}-->修改区域<!--{else}-->添加区域<!--{/if}-->" />&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn" onclick="E.closeWindows();" value="关闭" />
        </div>
    </div>



</div>

<script type="text/javascript">

//全局变量(城市联动)
var Flg;

var storeID = '<!--{$storeID}-->';

var params = new Array();

var regionModify = {

    dt: "",

    check: function() {

        this.dt = E.getFormValues("storeRegion_form");
        var error_msg = "";
        var checkboxNum = 0;

        $("input[name='regions']").each(function(){

            var id = $(this).attr('id');
            var t = document.getElementById(id).checked;

            if( t == true ){

                var val = $(this).val();
                var a = {id:val};
                params.push(a);
                checkboxNum++;

            }


        })

        this.dt =  E.concat(this.dt,{params:params});

        if( checkboxNum == 0 ){
            error_msg += "您还未添加区域<br />";
        }

        if (error_msg != "")
            E.alert(error_msg);
        else {
            this.add();
        }

    },

    add: function() {
        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/wms/store.ajax?operFlg=4",
            dataType: "JSON",
            data: this.dt,
            success: function( result_obj ) {

                    E.opener("store.load");
                    E.alert(result_obj.message , 2 , "E.closeWindows()");
            }
        });
    },

    addList:function( obj ){

        if( obj.code == 200 ){

            var params = obj.data;

            var html = '<option value="">请选择</option>';

            for(var i=0; i<params.length; i++){
                html += '<option value="' + params[i].id + '">' + params[i].name + '</option>';
            }

            if( Flg == 1 ){
                $("#cityList").html(html);
            }else if ( Flg == 2 ){
                $("#regionList").html(html);
            }

        }else{
            E.alert('系统错误，请与管理员联系');
        }

    },

    load: function() {
        $("#flex").flexReload();
    }

};


//变更省份，查询出相应的城市
$("#provinceList").change(function(){

    //获取当前选中的省份ID
    var provinceID = $(this).val();

    Flg = 1;

    //将区置空
    $("#regionList").html('<option value="">请选择</option>');

    jquery_ajax(5,{id:provinceID,flg:1},regionModify.addList);
})

//变更城市，查询出相应的区
$("#cityList").change(function(){

    //获取当前选中的城市ID
    var cityID = $(this).val();

    Flg = 2;

    jquery_ajax(5,{id:cityID,flg:2},regionModify.addList);
})

function doAddAll(){
    document.getElementById("provinceDiv").innerHTML = "&nbsp;";
    for(i=1; i< document.storeRegion_form.provinceList.length; i++){
        regionId = document.storeRegion_form.provinceList.options[i].value;
        regionName = document.storeRegion_form.provinceList.options[i].text;
        if ((i-1)%10 == 0){
            document.getElementById("provinceDiv").innerHTML += "<br>";
        }
        document.getElementById("provinceDiv").innerHTML +=
                "<input type='checkbox' name='regions' id='" + regionId + "' value='" +
                        regionId + "' checked='true' />" + regionName + "&nbsp;";

    }
}

//添加选择的区域
function addSelectRegion(){

    var regionID = '';
    var regionName = '';
    var level = 0;

    if (document.storeRegion_form.regionList.selectedIndex>0){
        regionID = document.storeRegion_form.regionList.options[document.storeRegion_form.regionList.selectedIndex].value;
        regionName = document.storeRegion_form.regionList.options[document.storeRegion_form.regionList.selectedIndex].text;
        level = 3;
    } else if (document.storeRegion_form.cityList.selectedIndex>0){
        regionID = document.storeRegion_form.cityList.options[document.storeRegion_form.cityList.selectedIndex].value;
        regionName = document.storeRegion_form.cityList.options[document.storeRegion_form.cityList.selectedIndex].text;
        level = 2;
    } else if (document.storeRegion_form.provinceList.selectedIndex>0){
        level = 1;
        regionID = document.storeRegion_form.provinceList.options[document.storeRegion_form.provinceList.selectedIndex].value;
        regionName = document.storeRegion_form.provinceList.options[document.storeRegion_form.provinceList.selectedIndex].text;
    }

    // 检查该地区是否已经存在
    exists = false;
    if( regionID != '' ){

        for (i = 0; i < document.storeRegion_form.elements.length; i++)
        {
            if (document.storeRegion_form.elements[i].type=="checkbox")
            {
                if (document.storeRegion_form.elements[i].value == regionID)
                {
                    exists = true;
                    E.alert('所选地区已被添加');
                }
            }
        }

    }else{
        E.alert('请先选择区域');
        return false;
    }


    if (!exists){

        if(level == 3){
            nodesCount = calDivNodesCount(document.getElementById("regionDiv"));
            if (nodesCount!= 0 && nodesCount%10 == 0){
                document.getElementById("regionDiv").innerHTML += '<br>';
            }
            document.getElementById("regionDiv").innerHTML += "<input type='checkbox' name='regions' id='" + regionID + "' value='" +
                    regionID + "' checked='true' />" + regionName + "&nbsp;";
        } else if(level == 2){

            nodesCount = calDivNodesCount(document.getElementById("cityDiv"));
            if (nodesCount!= 0 && nodesCount%10 == 0){
                document.getElementById("cityDiv").innerHTML += '<br>';
            }
            document.getElementById("cityDiv").innerHTML +=  "<input type='checkbox' name='regions' id='" + regionID + "' value='" +
                    regionID + "' checked='true' />" + regionName + "&nbsp;";
        } else if(level == 1){
            nodesCount = calDivNodesCount(document.getElementById("provinceDiv"));
            if (nodesCount!= 0 && nodesCount%10 == 0){
                document.getElementById("provinceDiv").innerHTML += '<br>';
            }
            document.getElementById("provinceDiv").innerHTML +=  "<input type='checkbox' name='regions' id='" + regionID + "' value='" +
                    regionID + "' checked='true' />" + regionName + "&nbsp;";
        }
    }

}

function calDivNodesCount(obj){
    var childNodes = obj.childNodes;
    var nodesCount = 0;
    for(i=0;i<childNodes.length;i++){
        if (childNodes[i].nodeType == 3){
            nodesCount++;
        }
    }
    return nodesCount;
}

function jquery_ajax(flg,data,func){
    $.ajax({
        type: "post",
        url: "/invokingAjax-backend/wms/store.ajax?operFlg="+flg,
        dataType: "json",
        data: data,
        success: function( result_obj ) {
            func(result_obj);
        },
        error : function(msg){
            alert('网络不给力，请重新尝试...');
        }
    });
}

</script>

</body>

</html>
